package com.learn.project2.config.aspectj;


import com.learn.project2.bean.MarketAdmin;
import com.learn.project2.bean.MarketLog;
import com.learn.project2.bean.MarketOrder;
import com.learn.project2.mapper.MarketLogMapper;
import com.learn.project2.mapper.MarketOrderMapper;
import com.learn.project2.model.bo.admin.admin.AdminCreateBO;
import com.learn.project2.model.bo.admin.admin.AdminUpdateBO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 切面类
 * @author stone
 * @date 2021/12/28 11:50
 */
@Aspect
@Component
public class OrderControllerAspect {

    @Autowired
    MarketLogMapper logMapper;
    @Autowired
    MarketOrderMapper orderMapper;


    @Pointcut("execution(* com.learn.project2.controller.admin.OrderController.delete(..))")
    public void orderDelete() {//只需要这个方法的方法名
    }

    @Transactional
    @Around("orderDelete()")
    public Object delete(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        //在删除用户之前，插入一条操作日志，默认是失败的
        Object[] args = proceedingJoinPoint.getArgs();
        HttpServletRequest request = ((HttpServletRequest) args[1]);
        Map map = ((Map) args[0]);
        Integer orderId = (Integer) map.get("orderId");



        String ip = request.getRemoteAddr();
        Date date = new Date();

        Subject subject = SecurityUtils.getSubject();
        MarketAdmin admin = (MarketAdmin) subject.getPrincipal();


        MarketOrder order = orderMapper.selectByPrimaryKey(orderId);
        MarketLog marketLog = new MarketLog(null,admin.getUsername(),ip,2,"删除",false,"订单编号"+order.getOrderSn(),null, date, date,false);


        logMapper.insertSelective(marketLog);

        //执行controller的delete方法
        Object proceed = proceedingJoinPoint.proceed();


        //修改为成功
        marketLog.setStatus(true);
        logMapper.updateByPrimaryKeySelective(marketLog);


        return proceed;
    }





}
